package com.project.myapplication;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.os.Handler;
import android.os.IBinder;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private SeekBar sb;
    private MyService.Mybinder mm;
    private Handler handler;
    private Button playb;
    private Boolean isRun=true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        handler = new Handler();
        sb = (SeekBar)findViewById(R.id.sb);
        playb = (Button)findViewById(R.id.play);
        Intent intent = new Intent("service");
        bindService(intent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                mm = (MyService.Mybinder)service;
                sb.setMax(mm.getfulltime());
                Thread t = new Thread(){
                    @Override
                    public void run() {
                        while (mm.getprogress()<=mm.getfulltime()&& isRun){
                            sb.setProgress(mm.getprogress());
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                };
                t.setDaemon(true);
                t.start();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        },BIND_AUTO_CREATE);
        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            private  int jindu ;
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                jindu = progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mm.gototime(jindu);
            }
        });
    }

    public void getlist(View view) {
        Intent lintent = new Intent("getlist");
        ArrayList mp3list = new ArrayList();
        Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
        Log.i("hahah",".............................list"+cursor.getCount());
        for(int i=0;i<cursor.getCount();i++){
            Mp3 mp = new Mp3();
            cursor.moveToNext();
            String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
            Log.i("hahah","........................."+url.toString());
            String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
            int ismusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));
            if(ismusic!=0){
                mp.setName(name.toString());
                mp.setUrl(url.toString());
                mp3list.add(mp);
            }
        }
        lintent.putExtra("m",mp3list);
        Log.d("hahah", "..................." + mp3list.toString());
        startActivityForResult(lintent, 10);
    }
    public void play(View view) {
        Intent intent = new Intent("service");
        if(playb.getText().equals("play")){
            intent.putExtra("action", 1);
            startService(intent);
            playb.setText("pause");
        }else {
            intent.putExtra("action", 2);
            startService(intent);
            playb.setText("play");
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==10&&resultCode==20){
           String url =  data.getStringExtra("url");
            mm.changesong(url);
            Log.i("hahah","................换个成功啦"+url);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isRun=false;
    }
}
